# coding: utf-8
import logging
import os
import re
from logging.handlers import TimedRotatingFileHandler

################################
# 日志配置
# 按天切换
################################
LOG_DIR = 'logs'
# ------------ root ------------
# 1.1 root
logger = logging.getLogger('root')
logger.setLevel(logging.INFO)
# 1.2 formatter
formatter = logging.Formatter(
    '%(asctime)s - %(filename)s[%(lineno)d] - %(levelname)s - %(message)s'
)
# 1.3.1 handler-root
handler_rotate_root = TimedRotatingFileHandler(
    filename=os.path.join(LOG_DIR, 'monitor1000'),
    when='D',
    interval=1,
    backupCount=100,
    encoding='utf-8'
)
handler_rotate_root.suffix = '%Y%m%d.logs'
handler_rotate_root.extMatch = re.compile(r'\^d{4}\d{2}\d{2}.logs$')
handler_rotate_root.setFormatter(formatter)
# 1.3.2 handler-console
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)

# 1.4 add haddler -> logger
logger.addHandler(handler_console)
logger.addHandler(handler_rotate_root)

# ------------ logger_alert ------------
# 1.1 logger_alert
logger_alert = logging.getLogger('alert')
logger_alert.setLevel(logging.INFO)
# 1.2 formatter
formatter_alert = logging.Formatter(
    '%(asctime)s %(message)s'
)
# 1.3.1 handler-root
handler_rotate_alert = TimedRotatingFileHandler(
    filename=os.path.join(LOG_DIR, 'alert'),
    when='D',
    interval=1,
    backupCount=100,
    encoding='utf-8'
)
handler_rotate_alert.suffix = '%Y%m%d.logs'
handler_rotate_alert.extMatch = re.compile(r'\^d{4}\d{2}\d{2}.logs$')
handler_rotate_alert.setFormatter(formatter_alert)

# 1.4 add haddler -> logger
logger_alert.addHandler(handler_rotate_alert)

# ------------ logger_alert ------------
# 1.1 logger_alert
logger_output = logging.getLogger('output')
logger_output.setLevel(logging.INFO)
# 1.2 formatter
formatter_output = logging.Formatter(
    '%(asctime)s %(message)s'
)
# 1.3.1 handler-root
handler_rotate_output = TimedRotatingFileHandler(
    filename=os.path.join(LOG_DIR, 'output'),
    when='D',
    interval=1,
    backupCount=100,
    encoding='utf-8'
)
handler_rotate_output.suffix = '%Y%m%d.logs'
handler_rotate_output.extMatch = re.compile(r'\^d{4}\d{2}\d{2}.logs$')
handler_rotate_output.setFormatter(formatter_output)

# 1.4 add haddler -> logger
logger_output.addHandler(handler_rotate_output)